package cn.sumpu.app.guoguojie.web.mvc.misc;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.LuckyBiz;
import cn.sumpu.app.guoguojie.constant.Channel;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.domain.LuckRank;
import cn.sumpu.app.guoguojie.domain.LuckTime;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.OnLineUserListener;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

@Controller
public class LuckyController {
    private final static Logger logger = LoggerFactory.getLogger(LuckyController.class);
    @Autowired
    private LuckyBiz            luckyBiz;
    @Autowired
    private CmsContentBiz       contentBiz;

    @RequestMapping(value = "/misc/luck/lucktime.htm")
    public String lucky(HttpServletRequest request) {
        return "misc/lucky/luckTime";
    }

    @RequestMapping(value = "/misc/luck/luckgrid.htm")
    public String luckyGrid(Integer page, Integer pagesize, String title, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        try {
            PageModel pageModel = luckyBiz.getLuckTime(page, pagesize, title, null);
            pw = response.getWriter();
            pw.print(JSONObject.fromObject(pageModel));
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/lucky/luckygrid.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/luck/addlucktime.htm")
    public String addLucky(HttpServletRequest request) {
        return "misc/lucky/saveLuckTime";
    }

    @RequestMapping(value = "/misc/luck/editluck.htm")
    public String editLuck(HttpServletRequest request, String id) {
        try {
            LuckTime luckTime = luckyBiz.getLuckTimeById(id);
            String ltime = luckTime.getLtime();
            int index = ltime.indexOf(" ");
            if (index > 0) {
                luckTime.setLtime(ltime.substring(0, index));
            }

            request.setAttribute("luckTime", luckTime);
        } catch (AppBusinessException e) {
            logger.error("/misc/luck/editluck.htm", e);
        }
        return "misc/lucky/saveLuckTime";
    }

    @RequestMapping(value = "/misc/luck/savelucktime.htm")
    public String saveAlbum(HttpServletResponse response, LuckTime luckTime) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter pw = response.getWriter();
            luckyBiz.saveLuckTime(luckTime);
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/luck/savelucktime.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/luck/luckrank.htm")
    public String miscLuckRank(HttpServletRequest request, String luckid) {
        request.setAttribute("luckid", luckid);
        return "misc/lucky/luckRank";

    }

    @RequestMapping(value = "/misc/luck/luckrankgrid.htm")
    public String luckRankGrid(Integer page, Integer pagesize, String luckid, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        try {
            PageModel pageModel = luckyBiz.getLuckRank(page, pagesize, luckid);
            pw = response.getWriter();
            pw.print(JSONObject.fromObject(pageModel));
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/luck/luckrankgrid.htm] fail", e);
            }
        }
        return null;
    }

    //停掉该期
    @RequestMapping(value = "/misc/luck/stopluck.htm")
    public String stopAlbum(HttpServletResponse response, String luckId) {
        try {
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter pw = response.getWriter();
            if (!Common.emptyStr(luckId)) {
                LuckTime luckTime = new LuckTime();
                luckTime.setIdentifier(luckId);
                luckTime.setStatus("end");
                luckyBiz.saveLuckTime(luckTime);
                pw.print("success");
            }

        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/luck/stopluck.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/luck/viewrank.htm")
    public String viewRank(HttpServletRequest request, Integer pageSize, Integer page) {

        try {
            LuckTime luckTime = luckyBiz.getStartLuckTime();
            if (!Common.empty(luckTime)) {
                PageModel pageModel = luckyBiz.getLuckRank(page, pageSize, luckTime.getIdentifier());
                request.setAttribute("luckTime", luckTime);
                request.setAttribute("pageModel", pageModel);
                Member member = OnLineUserListener.getSessionMember(request);
                if (!Common.empty(member)) {
                    LuckRank myRank = luckyBiz.getMemberRank(luckTime.getIdentifier(), member.getIdentifier());
                    request.setAttribute("myRank", myRank);
                }
            }
            List<CmsContent> lunBoList = contentBiz.getContentList(Channel.LUCK_LUNBO, 8);
            List<CmsContent> rebateList = contentBiz.getContentList("6c142f9e-cd43-4adf-94dc-0a6f27940fdf", 5);
            List<CmsContent> advList = contentBiz.getContentList(Channel.ADV_RIGHT, 2);
            List<CmsContent> lunBoList2 = contentBiz.getContentList("21fc4fd7-e929-468e-988b-200a836baf6c", 6);
            request.setAttribute("lunBoList", lunBoList);
            request.setAttribute("rebateList", rebateList);
            request.setAttribute("advList", advList);
            request.setAttribute("lunBoList2", lunBoList2);
        } catch (Exception e) {
            logger.error("[/luck/viewrank.htm] fail", e);
        }
        return "v3/luck/viewRank";
    }

    @RequestMapping(value = "/luck/historyrank.htm")
    public String historyRank(HttpServletRequest request, Integer pageSize, Integer page, String luckId) {
        try {
            LuckTime luckTime = null;
            if (Common.empty(luckId)) {
                luckTime = luckyBiz.getLastLuckTime();
            } else {
                luckTime = luckyBiz.getLuckTimeById(luckId);
            }
            if (!Common.empty(luckTime)) {
                PageModel pageModel = luckyBiz.getHistoryLuckRank(page, luckTime.getFinanum(), luckTime.getIdentifier());
                request.setAttribute("luckTime", luckTime);
                request.setAttribute("pageModel", pageModel);
                request.setAttribute("luckId", luckId);
            }
            PageModel history = luckyBiz.getLuckTime(page, 10, null, "end");
            request.setAttribute("history", history);
            List<CmsContent> rightAdv = contentBiz.getContentList("b4c0eb3e-3a16-46bc-9a75-91c5bfe7734d", 2);
            request.setAttribute("rightAdv", rightAdv);
        } catch (Exception e) {
            logger.error("[/luck/viewrank.htm] fail", e);
        }
        return "v3/luck/historyRank";
    }

    @RequestMapping(value = "/luck/luckranknum.htm")
    public String luckRankNum(HttpServletResponse response, HttpServletRequest request) {
        response.setContentType("text/html; charset=UTF-8");
        Member member = OnLineUserListener.getSessionMember(request);
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
            if (Common.empty(member)) {
                pw.print("请登录");
            } else {
                LuckRank luckRank = luckyBiz.getRankNum(member.getIdentifier());
                pw.print(JSONObject.fromObject(luckRank));
            }

        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/luck/luckranknum.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/luck/luckrank.htm")
    public String luckRank(HttpServletRequest request, HttpServletResponse response) {
        try {
            LuckTime startLuckTime = luckyBiz.getStartLuckTime();
            if (Common.empty(startLuckTime)) {
                response.setContentType("text/html; charset=UTF-8");
                PrintWriter pw = response.getWriter();
                pw.print("noLuckTime");
            } else {
                request.setAttribute("startLuckTime", startLuckTime);
                return "v3/luck/luckRank";
            }

        } catch (Exception e) {
            logger.error("[/luck/luckrank.htm] fail", e);
        }
        return null;
    }
}
